/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.recover;

import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import reportcenter.support.dbutil.JPAFactory;

/**
 *
 * @author renan
 */
public class TesteBackup {

    public static void backUpDatabase() {
        EntityTransaction tx = JPAFactory.getInstance().getManager().getTransaction();
        tx.begin();
        try {
            java.text.SimpleDateFormat todaysDate = new java.text.SimpleDateFormat("dd-MM-yyyy");
            String backupdirectory = "/apachederby/backup/" + todaysDate.format((java.util.Calendar.getInstance()).getTime());
            Query query = JPAFactory.getInstance().getManager().createNativeQuery("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE('" + backupdirectory + "')");
            query.executeUpdate();
            System.out.println("backed up database to " + backupdirectory);

            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
        }
    }

    public static void backUpDatabaseWithFreeze() {

        EntityTransaction tx = JPAFactory.getInstance().getManager().getTransaction();
        tx.begin();
        try {
            //Estado ideal para copia dos arquivos
            Query query = JPAFactory.getInstance().getManager().createNativeQuery("CALL SYSCS_UTIL.SYSCS_FREEZE_DATABASE()");
            query.executeUpdate();
            //Executa comando do sistema operacional
            //para copiar a base de dados

            //Depois de copiar a base de dados
            //restaura a condição original do SGBD
            query = JPAFactory.getInstance().getManager().createNativeQuery("CALL SYSCS_UTIL.SYSCS_UNFREEZE_DATABASE()");
            query.executeUpdate();

            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            tx.rollback();
        }
    }


}
